#include <modules\modules.hpp>

#ifndef execNow
#define execNow call compile preprocessfilelinenumbers
#endif

private ["_uid"];

//All client should have the Functions Manager initialized, to be sure.
if (isnil "BIS_functions_mainscope") then {
    createCenter sideLogic;
    BIS_functions_mainscope = (createGroup sideLogic) createUnit ["FunctionsManager", [0,0,0], [], 0, "NONE"];
	BIS_fnc_locations = compile preprocessFileLineNumbers "CA\modules\functions\systems\fn_locations.sqf";
};

waitUntil{!isNil "BIS_fnc_init"};

mso_version = "4.55";
diag_log format["MSO-%1 Version: %2", time, mso_version];

// reset player intialised variable
if (isnil {player getvariable "player_intialised"}) then {player setvariable ["player_intialised", 0, false]};

//Create the comms menu on all machines.
[] call BIS_fnc_commsMenuCreate; 

// Add briefing for MSO
execNow  "tmf\core\scripts\briefing.sqf";

if (isnil "enable_teamswitch") then { enable_teamswitch = false; };
enableSaving [false, false];
enableTeamSwitch enable_teamswitch;

CRB_MAPCLICK = "";

"RMM_MPe" addPublicVariableEventHandler {
	private ["_data","_locality","_params","_code"];
	_data = _this select 1;
	_locality = _data select 0;
	_params = _data select 1;
	_code = _data select 2;
	
	if (switch (_locality) do {
			case 0 : {true};
			case 1 : {isserver};
			case 2 : {not isdedicated};
			default {false};
	}) then {
		if (typename _code == typename "") then { call compile Format["_code = %1;", _code]; };
		if (isnil "_params") then { call _code } else {_params call _code};
	};
};

mso_menuname = "Multi-Session Operations";

if (isnil "mso_interaction_key") then {
	mso_interaction_key = [221,[false,false,false]];
};

"Custom Locations (" + worldName + ")" call mso_core_fnc_initStat;

"Tyrghen Function Container" call mso_core_fnc_initStat;
execNow "tmf\core\modules\tyr_fnccontainer\main.sqf";

#ifdef RMM_MP_RIGHTS
private ["_uid"];
if(isNil "mprightsDisable") then {mprightsDisable = 1;};
if(mprightsDisable == 1) then {
        "MP Rights disabled" call mso_core_fnc_initStat;
        _uid = getPlayerUID player;
		MSO_R_Players = [_uid];
		MSO_R_PlayerRoles = ["admin","member","officer","leader","highcommand","moderator","crew","air"];
} else {
        "MP Rights" call mso_core_fnc_initStat;
        execNow "tmf\core\modules\rmm_mp_rights\main.sqf";
};
#endif

if(isNil "mprightsDisable") then {
        "Default Rights" call mso_core_fnc_initStat;
        _uid = getPlayerUID player;
		MSO_R_Players = [_uid];
		MSO_R_PlayerRoles = ["admin","member","officer","leader","highcommand","moderator","crew","air"];
};

#ifdef RMM_DEBUG
"Debug" call mso_core_fnc_initStat;
execNow "tmf\core\modules\rmm_debug\main.sqf";
#endif

#ifdef R3F_ACV
execNow "tmf\core\modules\R3F_acv\main.sqf";
#endif

#ifdef ADMINACTIONS
[player] execNow "tmf\core\modules\adminActions\main.sqf";
#endif

#ifdef TYR_TIMESYNC
[] execNow "tmf\core\modules\TYR_TimeSync\main.sqf";
#endif

#ifdef TYR_WEATHERSYNC
[] execNow "tmf\core\modules\TYR_WeatherSync\main.sqf";
#endif

setViewDistance 3000;
setTerrainGrid 12.5;
#ifdef RMM_SETTINGS
"View Distance Settings" call mso_core_fnc_initStat;
execNow "tmf\core\modules\rmm_settings\main.sqf";	
#endif

#ifdef TAW_VIEWDIST
"TAW View Distance" call mso_core_fnc_initStat;
execNow "tmf\core\modules\taw_vd\main.sqf";	
#endif

#ifdef TYR_MENU
[] execNow "tmf\core\modules\tyr_menu\main.sqf";
#endif

//*************************************************************
// Custom initialisations for the server, headless and client
//*************************************************************

if (isServer) then {
	"Server" call mso_core_fnc_initStat;
	[] execNow "tmf\core\scripts\init_server.sqf";
};

if (!isDedicated && !hasInterface) then {
	"Headless" call mso_core_fnc_initStat;
	[] execNow "tmf\core\scripts\init_hc.sqf";
};

if (!isDedicated) then {
	if (isnil "PAPABEAR") then { PAPABEAR = [playerSide,"HQ"]; };
	"Player" call mso_core_fnc_initStat;
	[] spawn (compile preprocessfilelinenumbers "tmf\core\scripts\init_player.sqf");
};